#include "Character.h"
#include "Constants.h"
#include <SDL/SDL.h>
#include <iostream>

using namespace std;

Character::Character(GLuint aCharacterTexture, GLuint aSpeachTexture, Pattern * aPattern, CubePileGrid * aWorld)
{
	pattern = aPattern;
	characterTexture = aCharacterTexture;
	speachTexture = aSpeachTexture;
	mouseX = 0;
	mouseY = 0;
	renderSpeachBubble = false;
	world = aWorld;
}

void Character::setYPos(int newY)
{
	yPos = newY;
}

void Character::render()
{
	glPushMatrix();
		glTranslatef(0, yPos, 0);
		glBindTexture(GL_TEXTURE_2D, characterTexture);
		glBegin(GL_QUADS);
			glColor4f(1, 1, 1, 1);
			
			glTexCoord2f(0, 1); glVertex3f(-1, 3, 0);
			glTexCoord2f(1, 1); glVertex3f(1, 3, 0);
			glTexCoord2f(1, 0); glVertex3f(1,-1, 0);
			glTexCoord2f(0, 0); glVertex3f(-1,-1, 0);
		
		glEnd();
		
		if(renderSpeachBubble)
		{
			glBindTexture(GL_TEXTURE_2D, speachTexture);
			glBegin(GL_QUADS);
				glColor4f(1, 1, 1, 1);
			
				glTexCoord2f(0, 1); glVertex3f(0.5, 5, 0.01);
				glTexCoord2f(1, 1); glVertex3f(3, 5, 0.01);
				glTexCoord2f(1, 0); glVertex3f(3, 2, 0.01);
				glTexCoord2f(0, 0); glVertex3f(0.5, 2, 0.01);
			glEnd();
			
			glTranslatef(1.0, 4.5, 0.11);
			float scale = 0.4;
			glScalef(scale, scale, scale);
			pattern->render();
		}
	
	glPopMatrix();
}

void Character::handleMouseClickEvent()
{
	if(!pattern->completed)
	{
		renderSpeachBubble = !renderSpeachBubble;
	}
}

void Character::moveTo(int x, int z)
{
	if(currentPile != NULL)
	{
		currentPile->setFeature(NULL);
	}
	currentPile = world->at(x,z);
	currentPile->setFeature(this);
}

void Character::animate(float timeD)
{
	
}

void Character::patternCompleted(Pattern * aPattern)
{
	renderSpeachBubble = false;
	moveTo(aPattern->getCharLeft(), aPattern->getCharTop());
}
